/**
 *
 */
package com.engine.Mangers;

import java.util.List;

import org.hibernate.Session;

import com.util.AWFLogger;
import com.util.EngineUtil;
import com.wfe.db.SessionUtil;
import com.wfe.db.WorkFlowEngineQueryUtil;
import com.wfe.mapping.client.WfPlanTask;
import com.wfe.mapping.client.WfProject;
import com.wfe.mapping.client.WfProjectTask;
import com.wfe.mapping.client.WfProjectTaskStatus;
import com.wfe.mapping.client.WfTaskRouting;

/**
 * @author Kareem.Yehya
 * @since 25 March 2009 <br>
 *        * @version 1.0.0.1 <br>
 *        This Class Resposible for:- <br>
 * <br>
 *        1- Get the task routing info form routing table. <br>
 *        2- Change the tasks status to assigned. 3- if the router find end task will close the Project
 */
public class Router
{

	public Router()
	{

	}

	/**
	 * @author kareem_yehya
	 * @since 07 April 2009 <br>
	 *        Please do not change this stupied code there is a problem in JVM.
	 * 
	 * 
	 * 
	 * 
	 * */
	public static void route(WfProjectTask projectTask)
	{
		try
		{
			System.out.println("inside the router");

			WfProjectTaskStatus finishedStatus = projectTask.getWfProjectTaskStatus();
			// -------- get assigned status to assign the routing nodes
			WfProjectTaskStatus status = getWfProjectTaskStatusByID(4l); // assigned
			// ----------- orginal projecttask
			WfProjectTask mainTask = getWfProjectTaskByID(projectTask.getRecid());
			// --------------- project
			WfProject mainProject = getWfProjectByID(mainTask.getWfProject().getRecid());
			// ------ all project tasks
			java.util.List<WfProjectTask> allProjectTasks = getWfProjectTasks(mainProject);

			// --------------- which tasks the main task routing to routing list
			List<WfTaskRouting> routinglist = getWfTaskRouting(10, mainTask.getWfPlanTask());

			if(routinglist == null || routinglist.size() == 0)
			{
				System.out.println("No any Routing For The Task " + mainTask.getRecid());
				AWFLogger.warn("No any Routing For The Task " + mainTask.getRecid());

				projectTask.setIsProcessed(false);
				WfProjectTaskStatus noRoutingStatus = getWfProjectTaskStatusByID(8l); // no-routing
				projectTask.setWfProjectTaskStatus(noRoutingStatus);
				updateTask(projectTask);
				return;
			}// if end

			outer: for(WfTaskRouting routeTask : routinglist)
			{
				for(WfProjectTask task : allProjectTasks)
				{

					if(routeTask.getWfPlanTask2().getRecid().longValue() == task.getWfPlanTask().getRecid().longValue())
					{

						if(finishedStatus.getRecid().longValue() == 2 && !routeTask.getRouteif().booleanValue())// /failed and false "0"
						{
							task.setWfProjectTaskStatus(status);
							updateTask(task);
							break outer;
						}
						else if(finishedStatus.getRecid().longValue() == 1 && routeTask.getRouteif().booleanValue())// sucess and true
						{
							task.setWfProjectTaskStatus(status);
							updateTask(task);
							break outer;
						}

					}
				}
			}// for end
		}catch(Exception e)
		{
			e.printStackTrace();
		}

	}

	// @SuppressWarnings("deprecation")
	private static List<WfProjectTask> getWfProjectTasks(WfProject project)
	{

		Session session = null;
		try
		{

			session = SessionUtil.getSession();
			List<WfProjectTask> retList = WorkFlowEngineQueryUtil.getWfProjectTasksByProject(project, session);
			return retList;

		}

		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	// ******** for test
	public static void main(String[] args)
	{
		Router router = new Router();
		WfProjectTask mainTask = router.loadDummyTask(1);
		route(mainTask);

	}

	// ******** for test
	// @SuppressWarnings("deprecation")
	public WfProjectTask loadDummyTask(long taskID)
	{
		Session session = null;
		try
		{

			session = SessionUtil.getSession();
			WfProjectTask projectTask = WorkFlowEngineQueryUtil.getWfProjectTaskByID(session, taskID);

			return projectTask;

		}

		catch(Exception e)
		{
			System.out.println("Couldn't Load Expected Dummy Task");
			e.printStackTrace();
			return null;
		}finally
		{

			EngineUtil.closeSession(session);

		}

	}

	private static WfProject getWfProjectByID(long projectID)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProject project = WorkFlowEngineQueryUtil.getWfProjectByID(projectID, session);
			return project;

		}

		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	// @SuppressWarnings("deprecation")
	private static WfProjectTask getWfProjectTaskByID(long taskID)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProjectTask projectTask = WorkFlowEngineQueryUtil.getWfProjectTaskByID(session, taskID);
			return projectTask;

		}

		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	// @SuppressWarnings("deprecation")
	private static List<WfTaskRouting> getWfTaskRouting(int maxResults, WfPlanTask plantask)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			List<WfTaskRouting> retList = WorkFlowEngineQueryUtil.getWfTaskRoutingByWfPlanTask(session, plantask);
			return retList;

		}

		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	// @SuppressWarnings("deprecation")
	private static WfProjectTaskStatus getWfProjectTaskStatusByID(long statusID)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProjectTaskStatus projectTaskStatus = WorkFlowEngineQueryUtil.getWfProjectTaskStatusByID(statusID, session);

			return projectTaskStatus;

		}

		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	// @SuppressWarnings("deprecation")
	private static void updateTask(WfProjectTask task)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WorkFlowEngineQueryUtil.updateWfProjectTask(task, session);

		}

		catch(Exception e)
		{
			e.printStackTrace();

		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

}
